package 数据结构练习;

public class 八皇后问题 {
    public static void main(String[] args) {
        int[][] map = new int[8][8];
        int n = function(map,7);
        System.out.println(n);
    }

    public static int function(int[][] map,int n){
        if(n<0){
            return 1;
        }
        int result = 0;
        for(int i=0;i<8;i++){
            map[n][i] = 1;
            if (judge(map,n,i)){
                result = result + function(map,n-1);
            }else {
                map[n][i] = 0;
                continue;
            }
            map[n][i] = 0;
        }
        return result;
    }

    public static boolean judge(int[][] map,int n,int i){
        for(int k = n+1;k<=7;k++){
            if(map[k][i]==1){
                return false;
            }
            else if(i+(k-n)<=7 && map[k][i+(k-n)]==1){
                return false;
            }
            else if(i-(k-n)>=0 && map[k][i-(k-n)]==1){
                return false;
            }
        }
        return true;
    }
}
